{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Introduction to Computer Programming" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" }, "tags": [ "remove-cell" ] }, "source": [ "**CS1302 Introduction to Computer Programming**\n", "___" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Computer" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### What is a computer?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Is computer a calculator that is bigger and more advanced?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "
\n", "\"Calculator\n", "
A calculator on a computer.
\n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "If computer is a calculator, then, is [abacus](https://en.wikipedia.org/wiki/Abacus) the first computer invented? " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "
\n", "\"Abacus\n", "
Abacus - an ancient mechanical computing device.
\n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Is your [smartphone](https://en.wikipedia.org/wiki/Samsung_DeX) a computer? \n", "What defines a computer?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- In addition to performing arithmetic calculations, a computer is designed in such a way that \n", "- we can write different programs (in a process called **programming** or **software development**)\n", "- for the computer to execute to perform different tasks." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### What is the architecture of a computer?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "A computer contains three main hardware components:\n", "- Input device\n", "- Processing unit\n", "- Output device" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "#### Peripherals" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "
\n", "\"Computer\n", "
Computer Peripherals.
\n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Input and output devices connected to a computer are called *peripherals*. \n", "They allow users to interact with the computer in different ways." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "**Exercise** Some examples of output devices are:\n", "- Monitor\n", "- Speaker\n", "\n", "Can you give an awesome example in the following box?" ] }, { "cell_type": "markdown", "metadata": { "nbgrader": { "grade": true, "grade_id": "cell-3ea9d712eccdf31c", "locked": false, "points": 0, "schema_version": 3, "solution": true, "task": false }, "slideshow": { "slide_type": "-" } }, "source": [ "- 3D printer available at [CityU](https://www.cityu.edu.hk/lib/create/3dprint.htm)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "**Exercise** Some examples of input devices are:\n", "- Keyboard\n", "- Mouse\n", "\n", "Can you give an awesome example?" ] }, { "cell_type": "markdown", "metadata": { "nbgrader": { "grade": true, "grade_id": "cell-1c411172f0ed411b", "locked": false, "points": 0, "schema_version": 3, "solution": true, "task": false }, "slideshow": { "slide_type": "-" } }, "source": [ "- 3D scanner available at [CityU](https://www.cityu.edu.hk/lib/create/3dscan.htm)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "**Exercise** Many devices are both input and output device. Can you give at least 3 examples?" ] }, { "cell_type": "markdown", "metadata": { "nbgrader": { "grade": true, "grade_id": "cell-e1982fbce01506b3", "locked": false, "points": 0, "schema_version": 3, "solution": true, "task": false }, "slideshow": { "slide_type": "-" } }, "source": [ "- hard disk\n", "- CD/DVD Rom (writable)\n", "- touch screen" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Central Processing Unit" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "
\n", "\"CPU\"\n", "
An Intel CPU.
\n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "The brain of a computer is its processor unit, or the [**C**entral **P**rocesisng **U**nit (CPU)](https://en.wikipedia.org/wiki/Central_processing_unit). \n", "It is located on the [*motherboard*](https://en.wikipedia.org/wiki/Motherboard) and connects to different peripherals using different [*connectors*](https://en.wikipedia.org/wiki/Category:Computer_connectors)." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Two important components in the CPU are:\n", "- **A**rithmetic and **L**ogic **U**nit (**ALU**): Performs arithmetics like a calculator (but for binary numbers)\n", "- **C**ontrol **U**nit (**CU**): Directs the operations of the processor in executing a program." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Let's run a CPU Simulator below from a [GitHub project](https://github.com/pddring/cpu-simulator).\n", "- Note that all values are zeros in the RAM (**R**andom **A**cess **M**emory) initially.\n", "- Under Settings, click `Examples`$\\to $`Add two numbers`. Observe that the values in the RAM have changed.\n", "- Click `Run` at the bottom right-hand corner." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2020-08-23T03:52:31.770264Z", "start_time": "2020-08-23T03:52:31.753581Z" }, "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/html": [ "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%html\n", "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Programming" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### What is programming?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Programming is the process of writing programs. \n", "But what is a program?" ] }, { "cell_type": "markdown", "metadata": { "nbgrader": { "grade": false, "grade_id": "cell-7675978e85548d96", "locked": true, "schema_version": 3, "solution": false, "task": false }, "slideshow": { "slide_type": "fragment" } }, "source": [ "**Exercise** You have just seen a program written in [machine language](https://en.wikipedia.org/wiki/Machine_code). Where is it?" ] }, { "cell_type": "markdown", "metadata": { "nbgrader": { "grade": true, "grade_id": "cell-08298600f10cfc25", "locked": false, "points": 0, "schema_version": 3, "solution": true, "task": false }, "slideshow": { "slide_type": "-" } }, "source": [ "The first six lines of binary sequences in the RAM. The last line `End`s the program." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- The CPU is capable of carrying out \n", " - a set of instructions such as `Add`, `Subtract`, `Store`, etc.\n", " - for some numbers stored in the RAM.\n", "- Both the instructions and the numbers are represented as binary sequences.\n", "- E.g., in Intel-based CPU, the command for addition is like **00000011 00000100**" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Why computer uses binary representation?" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2020-11-27T05:14:52.762257Z", "start_time": "2020-11-27T05:14:52.758268Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/html": [ "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%html\n", "" ] }, { "cell_type": "markdown", "metadata": { "ExecuteTime": { "end_time": "2020-08-23T04:12:46.028621Z", "start_time": "2020-08-23T04:12:46.019721Z" }, "slideshow": { "slide_type": "fragment" } }, "source": [ "**Exercise** The first electronic computer, called [Electronic Numerical Integrator and Computer (ENIAC)](https://en.wikipedia.org/wiki/ENIAC), was programmed using binary circuitries, namely *switches* that can be either `On` or `Off`. \n", "\n", "
\n", "
\n", "\"Two\n", "
Programmers controlling the switches of ENIAC.
\n", "
\n", "
\n", "\n", "However, it did not represent values efficiently in binary. 10 binary digits (bits) was used to represent a decimal number 0 to 9. \n", "Indeed, how many decimals can be represented by 10 bits?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "nbgrader": { "grade": true, "grade_id": "cell-1023083cd0aca779", "locked": false, "points": 0, "schema_version": 3, "solution": true, "task": false }, "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "2 ** 10 # because there are that many binary sequences of length 10." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "As mentioned in the video, there are *International Standards* for representing characters:\n", "- [ASCII](https://en.wikipedia.org/wiki/ASCII) (American Standard Code for Information Interchange) maps English letters and some other symbols to 8-bits (8 binary digits, also called a byte). E.g., `A` is `01000001`.\n", "- [Unicode](https://en.wikipedia.org/wiki/Unicode) can also represent characters in different languages such as Chinese, Japanese...etc. " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "There are additional standards to represent numbers other than non-negative integers:\n", "- [2's complement format](https://en.wikipedia.org/wiki/Two%27s_complement) for negative integers (e.g. -123)\n", "- [IEEE floating point format](https://en.wikipedia.org/wiki/IEEE_754) for floating point numbers such as $1.23 x 10^{-4}$." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "**Why define different standards?**" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- Different standards have different benefits. ASCII requires less storage for a character, but it represents less characters.\n", "- Although digits are also represented in ASCII, the 2's complement format is designed for arithmetic operations." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Different generations of programming languages" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Machine language is known as the **1st Generation Programming Language**. " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "**Are we going to start with machine language?**\n", "Start with learning 2's complement and the binary codes for different instructions?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "No. Programmers do not write machine codes directly because it is too hard to think in binary representations. " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Instead, programmers write human-readable **mnemonics** such as **ADD**, **SUB**..., \n", "called **Assembly language**, or the **2nd Generation Programming Language**." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "
\n", "
\n", "\"Motorola\n", "
\n", " A Code written in an assembly language.\n", "
\n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "**Are you going to learn an assembly language?**" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Both machine language and assembly language are low-level languages which\n", "- are difficult to write for complicated tasks (requiring many lines of code), and\n", "- are platform-specific:\n", " - the sets of instructions and their binary codes can be different for different [types of CPUs](https://en.wikipedia.org/wiki/Comparison_of_CPU_microarchitectures), and\n", " - different operating systems use [different assembly languages/styles](https://en.wikipedia.org/wiki/X86_assembly_language)." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Anyone wants to learn assembly languages, and write a program in many versions to support different platforms?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Probably for programmers who need to write fast or energy-efficient code such as\n", "- a driver that controls a 3D graphics card, and\n", "- a program that control a microprocessor with limited power supply." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "But even in the above cases, there are often better alternatives. Play with the following microprocessor simulator:\n", "- Click `CHOOSE A DEMO`$\\to $`LED`.\n", "- Click `RUN SCRIPT` and observes the LED of the board.\n", "- Run the demos `ASSEMBLY` and `MATH` respectively and compare their capabilities.\n" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "ExecuteTime": { "end_time": "2020-08-23T12:30:45.988658Z", "start_time": "2020-08-23T12:30:45.980714Z" }, "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/html": [ "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%html\n", "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## High-level Language" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "ExecuteTime": { "end_time": "2020-08-23T12:31:08.305383Z", "start_time": "2020-08-23T12:31:08.296945Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/html": [ "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%html\n", "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Programmer nowadays use human-readable language known as the **3rd generation language (3GL)** or **high-level language.**\n", "- Examples includes: C, C++, Java, JavaScript, Basic, Python, PHP, ..." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### What is a high-level language?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- A code written in high-level language gets converted automatically to a low-level machine code for the desired platform. \n", "- Hence, it *abstracts* away details that can be handled by the computer (low-level code) itself." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "For instance, a programmer needs not care where a value should be physically stored if the computer can find a free location automatically to store the value." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Different high-level languages can have different implementations of the conversion processes:\n", "- **Compilation** means converting a program well before executing of the program. E.g., C++ and Java programs are compiled.\n", "- **Interpretation** means converting a program on-the-fly during the execution of a program. E.g., JavaScript and Python programs are often interpreted.\n", "\n", "Roughly speaking, compiled programs run faster but interpreted programs are more flexible and can be modified at run time. \n", "(The [truth](https://finematics.com/compiled-vs-interpreted-programming-languages/) is indeed more complicated than required for this course.)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### What programming language will you learn?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "You will learn to program using **Python**. The course covers:\n", "- Basic topics including *values*, *variables*, *conditional*, *iterations*, *functions*, *composite data types*.\n", "- Advanced topics that touch on functional and object-oriented programming.\n", "- Engineering topics such as *numerical methods*, *optimizations*, and *machine learning*.\n", "\n", "See the [course homepage](https://canvas.cityu.edu.hk/courses/42868) for details." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "**Why Python?**" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "ExecuteTime": { "end_time": "2020-08-23T10:59:22.898373Z", "start_time": "2020-08-23T10:59:22.889998Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/html": [ "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%html\n", "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "In summary:\n", "- Python is *expressive* and can get things done with fewer lines of code as compared to other languages.\n", "- Python is *popular*. It has an extensive set of libraries for Mathematics, graphics, AI, Machine Learning, etc.\n", "- Python is *free and open source*, so you get to see everything and use it without restrictions.\n", "- Python is *portable*. The same code runs in different platforms without modifications." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "**How does a Python program look like?**" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "ExecuteTime": { "end_time": "2020-11-27T11:20:04.656873Z", "start_time": "2020-11-27T11:20:04.651575Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# for step-by-step execution using mytutor\n", "%reload_ext mytutor" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "ExecuteTime": { "end_time": "2020-11-27T11:20:05.792756Z", "start_time": "2020-11-27T11:20:05.786145Z" }, "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%mytutor -h 400\n", "# The program here reads the cohort and reports which year the user is in\n", "# Assumption: Input is integer no greater than 2020\n", "import datetime # load a library to tell the current year\n", "\n", "cohort = input(\"In which year did you join CityU? \")\n", "year = datetime.datetime.now().year - int(cohort) + 1\n", "print(\"So you are a year\", year, \"student.\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "A Python program contains *statements* just like sentences in natural languages. \n", "E.g., `cohort = input(\"In which year did you join CityU? \")` is a statement that gives some value a name called `cohort`." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "For the purpose of computations, a statement often contains *expressions* that evaluate to certain values. \n", "E.g., `input(\"In which year did you join CityU? \")` is an expression with the value equal to what you input to the prompt. \n", "That value is then given the name `cohort`." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Expressions can be composed of:\n", "- *Functions* such as `input`, `now`, and `int`, etc., which are like math functions the return some values based on its arguments, if any.\n", "- *Literals* such as the string `\"In which year did you join CityU? \"` and the integer `1`. They are values you type out literally.\n", "- *Variables* such as `cohort` and `year`, which are meaningful names to values." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "To help others understand the code, there are also *comments* that start with `#`. \n", "These are descriptions meant for human to read but not to be executed by the computer." ] }, { "cell_type": "markdown", "metadata": { "nbgrader": { "grade": false, "grade_id": "cell-6c906df220a97280", "locked": true, "schema_version": 3, "solution": false, "task": false }, "slideshow": { "slide_type": "subslide" } }, "source": [ "**Exercise** What do you think the next generation programming should be?" ] }, { "cell_type": "markdown", "metadata": { "nbgrader": { "grade": true, "grade_id": "cell-205d445ede20461d", "locked": false, "points": 0, "schema_version": 3, "solution": true, "task": false }, "slideshow": { "slide_type": "fragment" } }, "source": [ "Perhaps programming using natural languages. Write programs that people enjoy reading, like [literate programming](https://www.youtube.com/watch?v=bTkXg2LZIMQ). \n", "Indeed, Jupyter notebook is arguably a step towards this direction. See [nbdev](https://github.com/fastai/nbdev)." ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "ExecuteTime": { "end_time": "2020-08-23T12:02:59.640220Z", "start_time": "2020-08-23T12:02:59.630355Z" } }, "outputs": [ { "data": { "text/html": [ "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%html\n", "" ] } ], "metadata": { "celltoolbar": "Slideshow", "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.12" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "rise": { "enable_chalkboard": true, "scroll": true, "theme": "white" }, "toc": { "base_numbering": 1, "nav_menu": { "height": "195px", "width": "330px" }, "number_sections": true, "sideBar": true, "skip_h1_title": true, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": { "height": "454.418px", "left": "1533px", "top": "110.284px", "width": "435.327px" }, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }